package com.indusfo.spc.service.impl;


import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.interceptor.UserThreadLocal;
import com.indusfo.spc.mapper.DepMapper;
import com.indusfo.spc.mapper.UserMapper;
import com.indusfo.spc.pojo.Dep;
import com.indusfo.spc.pojo.User;
import com.indusfo.spc.service.DepService;
import com.indusfo.spc.untlis.StringUtils;
import com.indusfo.spc.vo.JSONObject;
import com.indusfo.spc.vo.XCommonUtil;
import com.indusfo.spc.vo.XTreeNode;
import com.indusfo.spc.vo.XTreeUtil;
import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DepServiceImpl implements DepService {
  private static final Logger logger = LoggerFactory.getLogger(DepServiceImpl.class);

  @Override
  public int hashCode() {
    return super.hashCode();
  }

  @Resource
  private DepMapper depMapper;
  @Resource
  private UserMapper userMapper;

  /**
   * 新增修改
   *
   * @param dep
   * @return
   */
  @Override
  public JSONObject creatDepts(Dep dep) {
    String successMsg = "";
    // 创建人id从本地线程类获得
    User user = UserThreadLocal.get();
    dep.setUserId(user.getUserId());
    if (dep.getDepPid() == null) {
      dep.setDepPid(0);
    }
    try {
      // 参数校验
      checkParam(dep);
      //新增
      if (dep.getDepId() == null || "".equals(dep.getDepId())) {
        Integer over = depMapper.insertDepts(dep);
        if (over < 1) {
          return JSONObject.build(505, "新增失败");
        }
        successMsg = "新增成功";
        return JSONObject.build(200, successMsg, dep.getDepId());
      } else {
        //修改
        if (dep.getDepPid() == null) {
          dep.setDepPid(0);
        }
        //判断部门编码是否更改
        int num = depMapper.judgeDepCode(dep);
        if (num == 0) {
          throw new ParamsErrorException("部门已被刪除");
        }
        Integer over = depMapper.updateDepts(dep);
        if (over < 1) {
          return JSONObject.build(505, "修改失败");
        }
        successMsg = "修改成功";
        return JSONObject.build(200, successMsg);
      }
    } catch (GlobalException e) {  //这里只捕获自定义异常
      logger.error(e.getMessage(), e);
      return JSONObject.build(403, e.getMessage());
    }

  }

  /**
   * 删除
   *
   * @param dep
   * @return
   */
  @Override
  public JSONObject deleteDepts(Dep dep) {
    String msg = "";
    try {
      if (dep.getDepId() == null) {
        throw new ParamsErrorException("部门id不能为空");
      }
      //查询是否有子集
      Integer count = depMapper.lookDeptsBylDepPar(dep);
      if (count > 0) {
        throw new ParamsErrorException("该部门下还有子集");
      }
      //查询部门下是否有用户
      User user = new User();
      user.setDepId(dep.getDepId());
      List<User> users = userMapper.listUserDep(user);
      if (users.size() > 0) {
        throw new ParamsErrorException("该部门下还有用户");
      }
      // 执行删除
      int row = depMapper.deleteDepts(dep);
      if (row == 0) {
        new ModifyFailedException("删除失败");
      }
      msg = "删除成功";
    } catch (GlobalException e) {
      logger.error(e.getMessage(), e);
      return JSONObject.build(403, e.getMessage());
    }
    return JSONObject.build(200, msg);
  }



  @Override
  public List<Dep> DeptZtrees(Dep dep) {
    return depMapper.lookDepts(dep);
  }

  @Override
  public List<Dep> selectDepByName(String depName) {
    return depMapper.selectDepByName(depName);
  }


  /**
   * 参数校验
   *
   * @param dep
   */
  private void checkParam(Dep dep) {
    if (StringUtils.isEmpty((dep.getDepCode()))) {
      throw new ParamsErrorException("部门编码不能为空");
    }
    if (dep.getDepCode().getBytes().length > 50) {
      throw new ParamsErrorException("部门编码长度过长,限制50");
    }
    if (("").equals(dep.getDepName())) {
      throw new ParamsErrorException("部门名称不能为空");
    }
    if (("全部").equals(dep.getDepName())) {
      throw new ParamsErrorException("该部门名称已存在");
    }
    if (dep.getDepName().getBytes().length > 50) {
      throw new ParamsErrorException("部门名称长度过长,限制50");
    }
    //编码重复与否
    Integer repeat = depMapper.repeat(dep);
    if (repeat > 0) {
      throw new ParamsErrorException("编码不能重复");
    }
    //名称重复与否
    Integer repeat1 = depMapper.repeatName(dep);
    if (repeat1 > 0) {
      throw new ParamsErrorException("名称不能重复");
    }
  }
}
